package view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Turtle {
	
	ResourceBundle myViewLabels = ResourceBundle.getBundle("resources.englishView");
	ResourceBundle mySizes = ResourceBundle.getBundle("resources.viewSizes");
	ResourceBundle myIcons = ResourceBundle.getBundle("resources.slogoMultimedia");
	ResourceBundle myAcceptedTypes = ResourceBundle.getBundle("resources.slogoMultimedia");
	
	private JFrame turtleFrame;
	private String turtlePath;
	private Image img;
	private Image resizedImg;
	JRadioButton radioButton;
	private File selectedFile;
	
	public void execute()
	{
		turtleFrame = new JFrame(myViewLabels.getString("TurtleWindow"));
		turtleFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		ImageIcon basicTurtle = new ImageIcon(myIcons.getString("Turtle1"));
		basicTurtle = resizeIcon(basicTurtle, 
				Integer.parseInt(mySizes.getString("TurtleIconWidth")), 
				Integer.parseInt(mySizes.getString("TurtleIconHeight")));
		JRadioButton turtleB = makeRadioButton(basicTurtle, 
				myIcons.getString("Turtle1"), true);
		
		ImageIcon cartoonTurtle = new ImageIcon(myIcons.getString("Turtle2"));
		cartoonTurtle = resizeIcon(cartoonTurtle, 
				Integer.parseInt(mySizes.getString("TurtleIconWidth")), 
				Integer.parseInt(mySizes.getString("TurtleIconHeight")));
		JRadioButton turtleC = makeRadioButton(cartoonTurtle, 
				myIcons.getString("Turtle2"), false);
		
		ImageIcon photoTurtle = new ImageIcon(myIcons.getString("Turtle3"));
		photoTurtle = resizeIcon(photoTurtle, 
				Integer.parseInt(mySizes.getString("TurtleIconWidth")), 
				Integer.parseInt(mySizes.getString("TurtleIconHeight")));
		JRadioButton turtleP = makeRadioButton(photoTurtle, 
				myIcons.getString("Turtle3"), false);
		
		ButtonGroup turtles = new ButtonGroup();
		turtles.add(turtleB);
		turtles.add(turtleC);
		turtles.add(turtleP);
		
		JPanel radioPanel = new JPanel(new BorderLayout());
		radioPanel.add(turtleB, BorderLayout.WEST);
		radioPanel.add(turtleC, BorderLayout.CENTER);
		radioPanel.add(turtleP, BorderLayout.EAST);
		radioPanel.setBorder(BorderFactory.createTitledBorder(
		           BorderFactory.createEtchedBorder(), 
		           myViewLabels.getString("TurtleSetFrame")));
		
		JPanel choosePanel = new JPanel(new BorderLayout());
		JButton chooseFileButton = new JButton(myViewLabels.getString("TurtleChooseButton"));
		chooseFileButton.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
			    
				FileNameExtensionFilter filter = new FileNameExtensionFilter(
			        myAcceptedTypes.getString("PictureFileFilter"), 
			        myAcceptedTypes.getString("AcceptedPicType1"),
			        myAcceptedTypes.getString("AcceptedPicType2"));
			    chooser.setFileFilter(filter);
			    
			    Component parent = null;
				int returnVal = chooser.showOpenDialog(parent);
			    if(returnVal == JFileChooser.APPROVE_OPTION)
			    {
			    	selectedFile = chooser.getSelectedFile();
			    }
				turtleFrame.dispose();
			}
			
		});
		
		choosePanel.add(chooseFileButton, BorderLayout.CENTER);
		choosePanel.setBorder(BorderFactory.createTitledBorder(
		           BorderFactory.createEtchedBorder(), 
		           myViewLabels.getString("TurtleChooseFrame")));
		
		turtleFrame.add(radioPanel, BorderLayout.WEST);
		turtleFrame.add(choosePanel, BorderLayout.EAST);
		turtleFrame.pack();
		turtleFrame.setVisible(true);
	}
	
	public ImageIcon resizeIcon(ImageIcon icon, int width, int height)
	{
		img = icon.getImage();
		resizedImg = img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
		ImageIcon newIcon = new ImageIcon(resizedImg);
		return newIcon;
	}
	
	private JRadioButton makeRadioButton(ImageIcon icon, String actionCommand, boolean selected)
	{
		radioButton = new JRadioButton(icon, selected);
		radioButton.setActionCommand(actionCommand);
		radioButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				turtlePath = e.getActionCommand();
				turtleFrame.dispose();
			}
		});
		return radioButton;
	}
	
	public String returnSelectedTurtle()
	{
		return turtlePath;
	}
}
